<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户名称">
          <a-input placeholder="请输入客户名称" v-decorator="['customerName', {rules:[{required:true,message:'请输入'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="客户图片">
          <!-- <a-input placeholder="请输入客户图片" v-decorator="['imgPath', {}]" /> -->
          <a-upload
            name="file"
            :multiple="false"
            :accept="accept"
            :showUploadList="false"

            v-decorator="['imgPath', {}]"
            :beforeUpload="uploadPicBp">
            <a-button><a-icon type="upload"/>上传</a-button>
          </a-upload>
          <span style="margin-left:20px;" v-for="(item,index) in fileListBp">{{item.name}}<a-icon style="cursor:pointer;margin-left:20px;" type="delete" @click="deleteFileObjBp(index)"/></span>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction,postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "RelationCompanyCustomerModal",
    data () {
      return {
        fileListBp:[],
        accept:'image/*',
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/tc/relationCompanyCustomer/add",
          edit: "/tc/relationCompanyCustomer/edit",
          updateFile:'/tc/relationCompanyCustomer/upload',
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.fileListBp = [];
        if(this.model.id && this.model.imgPath){
          let name = this.model.imgPath?this.model.imgPath.split('/')[this.model.imgPath.split('/').length-1]:''
          let fileObj ={
            name:name
          }
          this.fileListBp.push(fileObj)
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'customerName','imgPath'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      deleteFileObjBp(index){
        this.fileListBp = [];
        this.form.setFieldsValue({
            imgPath: '',
        })
      },
      uploadPicBp(fileObj){
        let httpurl = this.url.updateFile;
        let formData = new FormData();
        formData.append('file',fileObj)
        let that = this;
        this.fileListBp = [];
        postAction(httpurl,formData).then((res)=>{
          if(res.success){
            that.$message.success('上传成功');
            this.form.setFieldsValue({
                imgPath: res.message,
            })
            this.fileListBp.push({name:fileObj.name})
            console.log('ssss',this.fileList)
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
          // that.close();
        })
        return false;
      },

    }
  }
</script>

<style lang="less" scoped>

</style>